﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Exceptions;
using Acms.Bll;

public partial class Templates_Register_Default : BaseTemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            valPostCodeFormat.ValidationExpression = School.PostCodeValidationExpression;
            valEmailAddressFormat.ValidationExpression = School.EmailValidationExpression;
            valPasswordFormat.ValidationExpression = School.PasswordValidationExpression;
            pnlLoggedIn.Visible = Request.IsAuthenticated;
            pnlLogin.Visible = pnlRegister.Visible = !Request.IsAuthenticated;
            if (!string.IsNullOrEmpty(Request.QueryString["Return Url"]))
            {
                lnkContinue.NavigateUrl = Page.ResolveUrl(Request.QueryString["Return Url"]);
            }
            else
            {
                lnkContinue.NavigateUrl = Page.ResolveUrl("~/");
            }
            DataBind();
        }

    }

    protected void btnEnter_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            //check school not already in db
            List<User> tmp = Acms.Bll.User.GetAllByCustomProperty(AcmsContext.SiteId, "School Name", txtSchoolName.Text.Trim());
            if (tmp != null && tmp.Count > 0)
            {
                pnlErrors.Visible = true;
                ulErrors.InnerHtml = string.Format("<li>{0}</li>", "This school has already been registered");
                return;
            }
            Acms.Bll.User user = Acms.Bll.User.CreateInstance(AcmsContext.CurrentPost.SiteId);
            user.Address1 = txtAddress1.Text.Trim();
            user.Address2 = txtAddress2.Text.Trim();
            user.EmailAddress = txtEmailAddress.Text.Trim();
            user.FirstName = txtContactFirstName.Text.Trim();
            user.LastName = txtContactLastName.Text.Trim();
            user.MobileTel = txtContactTel.Text.Trim();
            user.PostCode = txtPostCode.Text.Trim();
            user.Town = txtTown.Text.Trim();
            user.County = txtCounty.Text.Trim();
            user.UserName = txtUsername.Text.Trim();
            user.SetCustomProperty("School Name", txtSchoolName.Text.Trim());
            user.SetCustomProperty("School Type", ddlType.SelectedValue);
            user.SetCustomProperty("School Tel", txtSchoolTel.Text.Trim());
            user.SetCustomProperty("Secondary Contact Name", txtSecondaryContact.Text.Trim());

            user.SetPassword(null, txtPassword.Text.Trim());
            user.SetLogedOn();
            try
            {
                user.Update();
            }
            catch (DuplicateEmailException)
            {
                pnlErrors.Visible = true;
                ulErrors.InnerHtml = string.Format("<li>{0}</li>", "This email address has already been registered");
                return;
            }
            catch (DuplicateUsernameException)
            {
                pnlErrors.Visible = true;
                ulErrors.InnerHtml = string.Format("<li>{0}</li>", "This username has already been registered");
                return;
            }

            Guid key = user.GetLoginKey(false);
            System.Web.Security.FormsAuthentication.SetAuthCookie(key.ToString(), false);

            pnlLogin.Visible = pnlRegister.Visible = false;
            pnlEntry.Visible = true;

            /*School tmp = new School();
            tmp.Password = txtPassword.Text.Trim();

            try
            {
                tmp.Save();
            }
            catch (ValidationException ex)
            {
                System.Text.StringBuilder messages = new System.Text.StringBuilder();
                foreach (ValidationExceptionItem item in ex.Items)
                {
                    messages.AppendFormat("<li>{0}</li>", item.Message);
                }
                pnlErrors.Visible = true;
                ulErrors.InnerHtml = messages.ToString();
                return;
            }

            //Send the email
            Acms.Bll.Email email = Acms.Bll.Email.CreateInstance(tmp.ContactDisplayName, tmp.ContactEmailAddress, Server.MapPath("~/Emails/SchoolRegisterdEmail.xml"));
            email.Format("SchoolName", tmp.SchoolName);
            email.Send();

            pnlForm.Visible = false;
            pnlEntry.Visible = true;
            */
        }
    }
}
